package org.itboys.ace.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.admin.entity.AdminPost;
import org.itboys.admin.service.AdminPostService;
import org.itboys.admin.tools.AdminPermissionCheck;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.AceJsonPageUtils;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.AceUtils;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/ace/admin/post")
public class AceAdminPostController extends BaseController {
	@Autowired
	private AdminPostService adminPostService;
	
	/**
	 * 职务管理vm页面跳转
	 * @return
	 */
	@RequestMapping("/post-list")
	public ModelAndView postList(HttpServletRequest request, HttpServletResponse response, Model model) {
		return new ModelAndView("/ace/admin/post-list");
	}
	
	/**
	 * 分页查询职务管理列表
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping("/list")
	@AdminPermissionCheck("adminPost:list")
	public void list(HttpServletRequest request, HttpServletResponse response,Model model) {
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		if (null != param.get("name")) {
			param.put("name", AceUtils.getPattern(param.get("name")));
		}
		Page<AdminPost> page = adminPostService.pageQuery(param, request);
		AceJsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}
	
	/**
	 * 保存
	 * @param adminPost
	 * @param request
	 * @param response
	 */
	@RequestMapping("/savePost")
	public void add(@ModelAttribute AdminPost adminPost,HttpServletRequest request,HttpServletResponse response) {
		try{
			if(adminPost.getId() == 0l){
				adminPostService.insert(adminPost);
			}else{
				adminPostService.update(adminPost);
			}
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}catch(Exception e){
			logger.error("AdminPost save error", e);
			e.printStackTrace();
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	/***
	 * 根据id查询菜单信息
	 * 
	 * @param id
	 * @param response
	 */
	@RequestMapping("/getPostById/{id}")
	public void getOrg(@PathVariable("id") Long id, HttpServletResponse response) {
		AjaxUtils.renderJson(response, adminPostService.getById(id));
	}

	
	/**
	 * 删除
	 * @param id
	 * @param response
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public void delete(@PathVariable("id") Long id, HttpServletResponse response){
		try {
			adminPostService.delete(id);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("AdminPost delete error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
}
